package org.neuedu.fly.service;

import org.apache.ibatis.session.SqlSession;
import org.neuedu.fly.entity.Post;
import org.neuedu.fly.entity.vo.ResponseEntity;
import org.neuedu.fly.mapper.PostMapper;
import org.neuedu.fly.mapper.ReplyMapper;
import org.neuedu.fly.util.SqlSessionUtil;

import java.util.List;
import java.util.Objects;

public class PostService {
    private SqlSession session = null;
    private PostMapper postMapper = null;
    private static final int NULLField = -1;

    public ResponseEntity page(int pageIndex, int pageSize) {
        session = SqlSessionUtil.getSqlSession();
        postMapper = session.getMapper(PostMapper.class);
        pageIndex = (pageIndex - 1) * pageSize;
        List<Post> data = postMapper.page(pageIndex,pageSize);
        int count = postMapper.count(-1,-1,-1,null);
        return ResponseEntity.page(count,data);
    }

    public ResponseEntity indexPage(String oldIndex, String oldSize, String Closed, String Fine, String sColumnName) {
        int pageIndex = 1, pageSize = 27, isClosed = -1, isFine = -1;
        pageIndex = Integer.parseInt(oldIndex);
        pageSize = Integer.parseInt(oldSize);
        if (Objects.nonNull(Closed)) {
            isClosed = Integer.parseInt(Closed);
        }
        if (Objects.nonNull(Fine)) {
            isFine = Integer.parseInt(Fine);
        }
        pageIndex = (pageIndex - 1) * pageSize;
        return indexPageAndIsTopPage(pageIndex, pageSize, isClosed, isFine, NULLField, sColumnName);
    }

    public ResponseEntity editPost(String postId,String status,String top, String Fine) {
        int isStatus = -1,isTop = -1, isFine = -1;
        if (Objects.nonNull(status)) {
            isStatus = Integer.parseInt(status);
        }
        if (Objects.nonNull(top)) {
            isTop = Integer.parseInt(top);
        }
        if (Objects.nonNull(Fine)) {
            isFine = Integer.parseInt(Fine);
        }
        session = SqlSessionUtil.getSqlSession();
        postMapper = session.getMapper(PostMapper.class);
        boolean flag = postMapper.edit(Integer.parseInt(postId),isStatus,isTop,isFine);
        session.commit();
        if (!flag){
            session.rollback();
            return ResponseEntity.error("出现错误");
        }
        return ResponseEntity.ok("修改成功");
    }

    public ResponseEntity isTopPage(int isTop) {
        return indexPageAndIsTopPage(NULLField, NULLField, NULLField, NULLField, isTop, null);
    }

    public ResponseEntity indexPageAndIsTopPage(int pageIndex, int pageSize, int isClosed, int isFine, int isTop, String columnName) {
        session = SqlSessionUtil.getSqlSession();
        session.clearCache();
        postMapper = session.getMapper(PostMapper.class);
        int count = postMapper.count(isClosed, isFine, isTop,columnName);
        List<Post> data = postMapper.indexPage(pageIndex, pageSize, isClosed, isFine, isTop, columnName);
        return ResponseEntity.page(count, data);
    }

    public ResponseEntity hotPost() {
        session = SqlSessionUtil.getSqlSession();
        postMapper = session.getMapper(PostMapper.class);
        return ResponseEntity.data(postMapper.hotPost());
    }

    public ResponseEntity sendPost(String userId, String columnId, String title, String context, String kiss) {
        session = SqlSessionUtil.getSqlSession();
        postMapper = session.getMapper(PostMapper.class);
        Post post = Post.builder().userId(Integer.parseInt(userId)).columnId(Integer.parseInt(columnId)).title(title).context(context).kiss(Integer.parseInt(kiss)).build();
        boolean flag = postMapper.sendPost(post);
        session.commit();
        if (!flag){
            session.rollback();
            return ResponseEntity.error("出现错误");
        }
        return ResponseEntity.ok();
    }

    public ResponseEntity findPostByUserId(Integer userId, Integer pageIndex, Integer pageSize) {
        session = SqlSessionUtil.getSqlSession();
        postMapper = session.getMapper(PostMapper.class);
        //数据处理
        pageIndex = (pageIndex - 1) * pageSize;
        //方法调用
        List<Post> list = postMapper.findPostByUserId(userId, pageIndex, pageSize);
        System.out.println(list);
        long count = postMapper.userCount(userId);
        return ResponseEntity.page(count, list);
    }

    public ResponseEntity findReplyByPostId(String postId) {
        session = SqlSessionUtil.getSqlSession();
        postMapper = session.getMapper(PostMapper.class);
        Post data = postMapper.findReplyByPostId(Integer.parseInt(postId));
        return ResponseEntity.data(data);
    }

    public ResponseEntity delPostByPostId(int postId) {
        session = SqlSessionUtil.getSqlSession();
        postMapper = session.getMapper(PostMapper.class);
        ReplyMapper replyMapper = session.getMapper(ReplyMapper.class);
        boolean ret = replyMapper.delByPostId(postId);
        boolean flg = postMapper.delByPostId(postId);
        session.commit();
        if (!flg || !ret){
            session.rollback();
            return ResponseEntity.error("出现错误");
        }
        return ResponseEntity.ok();
    }
}
